﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Aplicacion_Vista_Bodegas : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String nomRol = (String)Session["nombreRol"];
        String nomU = (String)Session["nombreUsuario"];

        if (nomRol == null || nomRol.Length <= 0 || nomU == null || nomU.Length <= 0)
        {
            Response.Redirect("Inicio.aspx");
        }

        //Verificacion de sesion---------------------------------------------------------

        ControladorBodegas c = new ControladorBodegas();
        c.cargarBodegas(GridView1);

        return;
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session["nombreRol"] = null;
        Session["nombreUsuario"] = null;
        Session["idRol"] = null;
        Session.Abandon();
        Response.Redirect("Inicio.aspx", true);
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ver materiales")
        {
            DataTable dt = (DataTable)GridView1.DataSource;
            int page = GridView1.PageIndex;
            int numPages = GridView1.PageSize;
            int preindex = Int16.Parse(e.CommandArgument.ToString());
            int index = preindex + page * numPages;
            int codigoBodega = Int16.Parse((String)(dt.Rows[index].ItemArray[2]));
            String nombreBodega = (String)(dt.Rows[index].ItemArray[0]);
            String deptoBodega = (String)(dt.Rows[index].ItemArray[1]);
            Response.Redirect("MaterialesEnBodega.aspx?i=" + codigoBodega.ToString() + "&n=" + nombreBodega + "&d="+deptoBodega+"");

        }

        if (e.CommandName == "editar bodega")
        {
            DataTable dt = (DataTable)GridView1.DataSource;
            int page = GridView1.PageIndex;
            int numPages = GridView1.PageSize;
            int preindex = Int16.Parse(e.CommandArgument.ToString());
            int index = preindex + page * numPages;
            int codigoBodega = Int16.Parse((String)(dt.Rows[index].ItemArray[2]));
            String nombreBodega = (String)(dt.Rows[index].ItemArray[0]);
            Response.Redirect("EditarBodega.aspx?i=" + codigoBodega.ToString() + "&n="+nombreBodega+"");
            
        }

        if (e.CommandName == "eliminar bodega")
        {
            DataTable dt = (DataTable)GridView1.DataSource;
            int page = GridView1.PageIndex;
            int numPages = GridView1.PageSize;
            int preindex = Int16.Parse(e.CommandArgument.ToString());
            int index = preindex + page * numPages;
            int codigoBodega = Int16.Parse((String)(dt.Rows[index].ItemArray[2]));
            String nombreBodega = (String)(dt.Rows[index].ItemArray[0]);
            String deptoBodega = (String)(dt.Rows[index].ItemArray[1]);
            Response.Redirect("EliminarBodega.aspx?i=" + codigoBodega.ToString() + "&n=" + nombreBodega + "&d="+deptoBodega+"");

        }
            
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
}